package com.cloud.controller;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Tangtao
 * @Date: 2022/5/8 19:10
 * @Description:
 * @ClassName:
 */

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class userInfoTestController {
    @Resource
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    private HttpHeaders httpHeaders;

    @Before
    public void loginByCardId (){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).
                apply(SecurityMockMvcConfigurers.springSecurity()).
                build();
        try{
            MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/user/loginByCardId")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content("{\"userId\":\"11111111111\",\"password\":\"abcd1234\"}")
                            .accept(MediaType.APPLICATION_JSON)
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            String[] items = content.split(",");
            String token = "";
            for(String item: items){
                String[] kv = item.split(":");
                if(kv[0].equals("\"token\"")){
                    int last = kv[1].lastIndexOf("\"");
                    token = kv[1].substring(1, last);
                }
            }
            System.out.println(token);
            Map<String, String> map = new HashMap<>();
            map.put("token", token);
            httpHeaders = new HttpHeaders();
            httpHeaders.setAll(map);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println(httpHeaders.get("token"));
            System.out.println(content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void GetInfoTest() {
        try{
            MvcResult mvcResult = mockMvc.perform(
                            MockMvcRequestBuilders.get("/user/getInfo")
                                    .headers(httpHeaders)
                                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            System.out.println("-------------------------------------------------------------------");
            System.out.println(status);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println("content" + content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    @Test
    public void updatePwd(){
        try{
            MvcResult mvcResult = mockMvc.perform(
                            MockMvcRequestBuilders.post("/user/updatePwd")
                                    .headers(httpHeaders)
                                    .contentType(MediaType.APPLICATION_JSON)
                                    .content("{\"oldPwd\":\"abcd4321\",\"newPwd\":\"abcd1234\"}")
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            System.out.println("-------------------------------------------------------------------");
            System.out.println(status);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println("content" + content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    @Test
    public void updateInfo(){
        try{
            MvcResult mvcResult = mockMvc.perform(
                            MockMvcRequestBuilders.post("/user/updateInfo")
                                    .headers(httpHeaders)
                                    .contentType(MediaType.APPLICATION_JSON)
                                    .content("{\"defaultRegion\":1,\"phoneNumber\":\"11111111111\"}")
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            System.out.println("-------------------------------------------------------------------");
            System.out.println(status);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println("content" + content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    @Test
    public void getAllUserInfo(){
        try{
            MvcResult mvcResult = mockMvc.perform(
                            MockMvcRequestBuilders.get("/user/getAllUserInfo")
                                    .headers(httpHeaders)
                                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            System.out.println("-------------------------------------------------------------------");
            System.out.println(status);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println("content" + content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    @Test
    public void userDelete(){
        try{
            MvcResult mvcResult = mockMvc.perform(
                            MockMvcRequestBuilders.post("/user/delete")
                                    .headers(httpHeaders)
                                    .contentType(MediaType.APPLICATION_JSON)
                                    .content("{\"userId\":\"88408d40cf7eb9747563629dfe5a21ee\",\"phoneNumber\":\"1111111111X\"}")
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            System.out.println("-------------------------------------------------------------------");
            System.out.println(status);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println("content" + content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void userUpdateType(){
        try{
            MvcResult mvcResult = mockMvc.perform(
                            MockMvcRequestBuilders.post("/user/updateType")
                                    .headers(httpHeaders)
                                    .contentType(MediaType.APPLICATION_JSON)
                                    .content("{\"userId\":\"88408d40cf7eb9747563629dfe5a21ee\",\"newType\":\"DOCTOR\"}")
                    )
                    .andDo(MockMvcResultHandlers.print()).andReturn();
            int status = mvcResult.getResponse().getStatus();
            String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
            System.out.println("-------------------------------------------------------------------");
            System.out.println(status);
            Assert.assertEquals("success", 200, status);
            Assert.assertFalse("failed", status != 200);
            System.out.println("content" + content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


}

